<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3>10进制转为2进制</h3>
<h4>10进制转为任意进制</h4>

<script>
    class Stack{
        item = [];
        push(data){
            this.item.push(data)
        }
        pop(){
            return this.item.pop()
        }
        peek(){
            return this.item.at(-1)
        }
        isEmpty(){
            return this.item.length === 0
        }
        clear(){
            this.item = []
        }
        size(){
            return this.item.length
        }
        toString(){
            return this.item.join('-')
        }
    }

    // function convent(decNumber){
    //     let remStack = new Stack()
    //     let number = decNumber
    //     let str = ''
    //
    //     while(number > 0){ // 大于0就一直循环
    //         remStack.push(number % 2)
    //         number = Math.floor(number / 2) // 向下取整
    //     }
    //     while(!(remStack.isEmpty())){ // 不是空的累加值
    //         str += remStack.pop()
    //     }
    //     return str
    // }
    // convent(50)
    function convent(decNumber,base){
        let remStack = new Stack()
        let number = decNumber
        let str = '';
        let baseStr = "0123456789ABCDEF" // 16进制表示法

        while(number>0){ // 除以2不等于0就一直循环
            remStack.push(number % base)
            number = Math.floor(number / base) // 向下取整
        }
        while(!(remStack.isEmpty())){ // 不是空的累加值
            str += baseStr[remStack.pop()]
        }
        return str
    }
    convent(500,16) // 1F4
</script>
</body>
</html>
